//
//  KFDSetNicknamePasswordViewController.m
//  
//
//  Created by JIRENTIANXIANG on 6/15/15.
//
//

#import "KFDSetNicknamePasswordViewController.h"
#import "KFDUIConfigurator.h"
#import "ui_config.h"
#import "UIButton+KFD.h"
#import "KFDAccountManager.h"
#import "JTToast.h"

@interface KFDSetNicknamePasswordViewController ()

@property (strong,nonatomic) KFDUIConfigurator* uiConfigurator;

@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation KFDSetNicknamePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [self.uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"设置昵称和密码";
    self.view.backgroundColor=KFD_BG_COLOR;
    [self configureFooterView];
}

-(void)configureFooterView
{
    UIView* footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.uiConfigurator.WIDTH, 80)];
    UIButton* loginButton=[UIButton redButtonWithtitle:@"立即登录"];
    loginButton.center=CGPointMake(self.uiConfigurator.WIDTH*0.5, 40);
    [loginButton addTarget:self action:@selector(loginButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:loginButton];
    self.tableView.tableFooterView=footerView;
}

-(void)loginButtonPressed
{
    [self.nicknameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    NSString* nickname=self.nicknameTextField.text;
    NSString* password=self.passwordTextField.text;
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if ([accountManager checkNickname:nickname illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        if ([accountManager checkPassword:password illegalInputHandler:^(NSString *message) {
            [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
        }]) {
            KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
            [accountManager setNickname:nickname password:password successHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self goBack];
                });
            } failureHandler:^(KFDResponseBody *response) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
                });
            }];
        }
    }
}

-(void)goBack
{
    if (self.sourceViewController) {
        [self.navigationController popToViewController:self.sourceViewController animated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
